import os  # 导入python os 模块

MAX_ROOM_NUM = 100  # 最大房间数

# 全局变量
rooms = {}  # 房间字典
customers = []  # 顾客列表
user_accounts = {}  # 用户账号字典


def initial_rooms():
    for i in range(1, MAX_ROOM_NUM + 1):
        if i <= 33:
            room_type = 1
            price = 200
        elif i <= 66:
            room_type = 2
            price = 300
        else:
            room_type = 3
            price = 500
        rooms[i] = {
            "number": i,
            "room_type": room_type,
            "price": price,
            "state": 0
        }


def load_room_data():
    if os.path.exists("rooms.txt"):  # 检查房间数据文件是否存在
        with open("rooms.txt", "r") as infile:  # 打开房间数据文件
            for line in infile:
                room_info = line.split()  # 根据空格分割每行数据
                room_number = int(room_info[0])
                rooms[room_number] = {
                    "number": room_number,
                    "room_type": int(room_info[1]),
                    "price": int(room_info[2]),
                    "state": int(room_info[3])
                }  # 创建房间字典并添加到房间字典中
    else:
        initial_rooms()


def save_room_data():
    with open("rooms.txt", "w") as outfile:  # 打开房间数据文件
        for r in rooms.values():
            outfile.write(f"{r['number']} {r['room_type']} {r['price']} {r['state']}\n")  # 将房间信息写入文件


def load_account_data():
    if os.path.exists("user_accounts.txt"):  # 检查用户账号数据文件是否存在
        with open("user_accounts.txt", "r") as user_file:  # 打开用户账号数据文件
            for line in user_file:
                username, password = line.split()  # 根据空格分割每行数据
                user_accounts[username] = password  # 添加用户账号到用户账号字典中


def save_account_data():
    with open("user_accounts.txt", "w") as user_file:  # 打开用户账号数据文件
        for username, password in user_accounts.items():
            user_file.write(f"{username} {password}\n")  # 将用户账号信息写入文件


def welcome():
    os.system("cls")  # 清屏
    print("欢迎使用酒店管理系统")
    print("========================")
    print("请选择功能")
    print("1 -> 用户使用")
    print("0 -> 直接退出程序")
    identity_choice = input("请输入您的选择: ")

    if identity_choice == '1':
        user_login_menu()  # 用户登录区
        input("按任意键继续")
        welcome()
    elif identity_choice == '0':
        print("退出成功,期待您的下次使用！！")
        return
    else:
        print("无效选择!")
        input("按任意键返回主菜单")
        welcome()


def user_login_menu():
    os.system("cls")  # 清屏
    print("用户登录菜单:")
    print("   用户首次使用需要进行注册!!!!")
    print("1 -> 用户注册")
    print("2 -> 用户登录")
    user_choice = input("请输入您的选择: ")

    if user_choice == '1':
        user_register()  # 用户注册界面
    elif user_choice == '2':
        user_login()  # 用户登录界面
    else:
        print("无效选择!")
        input("按任意键返回主菜单")
        welcome()


def user_register():
    new_username = input("请输入新的用户名: ")
    new_password = input("请输入新的密码: ")
    user_accounts[
        new_username] = new_password  # 添加新的用户账号到用户账号字典中  user_accounts字典 new_username字典的一个键  new_password键所对应的值
    print("用户账号注册成功!")
    save_account_data()  # 保存账号数据到文件 save数据保存
    input("按任意键返回主菜单")
    user_menu()


def user_login():
    username = input("请输入用户名: ")
    password = input("请输入密码: ")
    if user_accounts.get(username) == password:  # 检查输入的用户名和密码是否与用户账号字典中的匹配
        print("用户登录成功!")
        input("按任意键继续")
        user_menu()
    else:
        print("用户名或密码错误！")
        input("按任意键返回主菜单")
        welcome()


def book_room():
    os.system("cls")
    name = input("请输入顾客姓名: ")
    ID = input("请输入顾客身份证号码: ")
    customer = {
        "name": name,
        "ID": ID,
        "room_number": 0,
        "prepaid": 0,
        "day": 0
    }  # 创建顾客字典
    customers.append(customer)  # 添加顾客到顾客列表中

    print("\n请选择房间类型:")
    print("1 -> 单人间/天  200元")
    print("2 -> 双人间/天  300元")
    print("0 -> 返回主菜单")
    room_type = input("请输入选择: ")

    if room_type == '0':
        welcome()
        return

    days = int(input("请输入预定天数: "))

    for room_number, r in rooms.items():
        if r["room_type"] == int(room_type) and r["state"] == 0:  # 检查房间类型是否匹配且房间状态是否为空闲
            r["state"] = 1  # 将房间状态设置为已预订
            customer["room_number"] = room_number  # 将顾客的房间号设置为预定成功的房间号
            customer["day"] = days  # 将顾客的入住天数设置为预定的天数
            customer["prepaid"] = r["price"] * days  # 计算顾客的预付款
            print(f"\n预订成功! 房间号为: {room_number} 预付款为: {r['price'] * days} 元")
            break
    else:
        print("很抱歉, 当前没有空闲的房间!")
    input("按任意键继续")
    user_menu()


def check_in():
    os.system("cls")
    room_number = int(input("\n请输入预定的房间号: "))

    for r in rooms.values():
        if r["number"] == room_number and r["state"] == 1:  # 检查房间号是否匹配且房间状态是否为已预订
            r["state"] = 2  # 将房间状态设置为已入住
            print(f"\n入住成功! 房间号为: {room_number}")
            break
    else:
        print("找不到该房间号或房间未预订!")
    input("按任意键继续")
    user_menu()


def user_menu():
    os.system("cls")
    print("用户操作菜单:")
    print("1 -> 预订房间")
    print("2 -> 办理入住")
    print("0 -> 返回主菜单")
    choice = input("请输入您的选择: ")

    if choice == '1':
        book_room()
    elif choice == '2':
        check_in()
    elif choice == '0':
        welcome()
    else:
        print("无效选择!")
        input("按任意键返回主菜单")
        user_menu()


# 程序入口
if __name__ == "__main__":
    load_room_data()
    load_account_data()
    welcome()
